package zd.cornermemory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import greendao.CjDao;
import zd.cornermemory.App;
import zd.cornermemory.R;
import zd.cornermemory.db.Cj;
import zd.cornermemory.db.GreenDaoManager;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;


/**
 * 欢迎界面
 */
public class SplashActivity extends BaseActivity {
    private CjDao cjDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        cjDao =  GreenDaoManager.getInstance().getNewSession().getCjDao();

        new Thread(new Runnable() {
            @Override
            public void run() {
                cjDao.detachAll();
                List<Cj> cjss = cjDao.queryBuilder().orderAsc(CjDao.Properties.Date).list();
                Map<Integer,List<Cj>> map = App.getInstance().getAllCjMap();
                map.clear();

                List<Cj> cjs;
                for (Cj cj : cjss) {
                    if(map.containsKey(cj.getGroup())){
                        cjs = map.get(cj.getGroup());
                    }else {
                        cjs = new ArrayList<Cj>();
                    }
                    cjs.add(cj);
                    map.put(cj.getGroup(), cjs);
                }

            }
        }).start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean isFirst = SPUtils.getInstance().getBoolean(SpKey.IS_FIRST, true);
                if(isFirst){
                    Intent intent = new Intent(mContext,FirstActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                    finish();
                }else {
                    Intent intent = new Intent(mContext,MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                    finish();
                }


            }
        }, 2000);
    }

}
